/ Assembly List / LJCDBDataAccess / DbDataAccess / Add

Namespace - LJCDBDataAccess


Returns

The database result XML message.

Syntax

C#
private DbResult Add()

Inserts a record with the specified columns. (E)

Remarks

This method is called from the Execute() method if the DbRequest.QueryType is "Insert".

Example

C#
// Adds a Person record.
private static void Add(DbDataAccess dbDataAccess
  , DbColumns dataDefinition)
{
  // Create the list of included columns.
  // This list must not include the database assigned columns
  // or the database assigned columns must have the AutoIncrement
  // value set to "true".
  List<string> columnNames = new List<string>();
  foreach (DbColumn column in dataDefinition)
  {
    if (column.ColumnName != "Id")
    {
      columnNames.Add(column.ColumnName);
    }
  }

  // The inserted columns must not include the DB assigned columns.
  Person dataRecord = new Person()
  {
    Name = "TestName",
    PrincipleFlag = false
  };

  // Create a Data Columns object with the included data definitions
  // and values from the data record.
  var dataColumns = DbCommon.QueryDataColumns(dataRecord, columnList
    , dataDefinition);

  // This code is needed only if there are database assigned columns.
  DbColumns keyColumns = GetKeyColumns(dataRecord, dataDefinition);

  // Create query with columns containing values from the record.
  DbRequest dbRequest = new DbRequest()
  {
    Columns = dataColumns,
    KeyColumns = keyColumns
    QueryType = QueryType.Insert.ToString(),
    TableName = "PersonTest",
  };
  DbResult dbResult = dbDataAccess.Execute(dbRequest);

  if (dbResult != null)
  {
    string sqlStatement = dbResult.ExecutedSql;

    // This code is needed only if there are database assigned columns.
    if (dbResult.DbRecords != null && dbResult.DbRecords.Count > 0)
    {
      SetAssignedValues(dataRecord, dbResult.DbRecords[0]);
    }
  }
}

// Gets the key columns if there are database assigned columns.
private static DbColumns GetKeyColumns(Person person
  , DbColumns dataDefinition)
{
  DbColumns retValue = null;

  // Create the list of database assigned and lookup column names.
  // This list must include the database assigned column definitions.
  List<string> lookupColumnNames = new List<string>()
  {
    "Id",
    "Name"
  };

  // Create a Lookup Keys object with the lookup data definitions
  // and values from the data record.
  // This is the unique key to find the inserted record for obtaining
  // the database assigned values.
  retValue = DbCommon.QueryDataKeys(person, dataDefinintion
    , lookupColumnNames);

  // Create the list of database assigned columns and updates
  // the key columns.
  List<string> dbAssignedColumnNames = new List<string>()
  {
    "Id"
  };
  DbCommon.SetDatabaseAssignedKeys(retValue, dbAssignedColumnNames);
  return retValue;
}

// Set the database assigned values.
private static void SetAssignedValues(Person person, DbValues resultRecord)
{
  Person addedPerson = new Person();
  DbCommon.SetObjectValues(resultRecord, addedPerson);
  person.ID = addedPerson.ID;
}